import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_screenutil/screen_util.dart';
import 'package:multi_image_picker2/multi_image_picker2.dart';

import 'package:flutter/material.dart';
import 'package:study_online/entity/Problem.dart';
import 'package:study_online/global/colors.dart';
import 'package:study_online/global/myIcons.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        //appBar: AppBar(
        //  backgroundColor: Colors.black26,
        //  title: Text('container'),
        //),
        body: Center(child: HomePage()),
      ),
    );
  }
}

class HomePage extends StatelessWidget {
  HomePage({Key key}) : super(key: key);
  Problem _problem = new Problem(
      "windChaser",
      "1",
      "#算法#flutter#回溯法 999999999999999999999999999999999999999999999999999999999999888888888888888888888weishenmai,wotainamle ,shijieshangmeiyou zhengzheng de gantongshenshou",
      12,
      3,
      "2021-10-9",
      "https://img2.baidu.com/it/u=172917783,2425047487&fm=26&fmt=auto");
  List<String> _labelList = [];

  @override
  Widget build(BuildContext context) {
    //debugPaintSizeEnabled=true;
    String tp = _problem.content;
    int num = 0;
    var pos = [0, 0];
    for (int i = 0; i < tp.length; ++i) {
      if (tp[i] == '#') {
        pos[num % 2] = i;
        num++;
        if (num >= 2)
          _labelList
              .add(tp.substring(min(pos[0], pos[1]) + 1, max(pos[0], pos[1])));
      }
    }
    for (int i = pos[0] + 2; i < tp.length; ++i) {
      if (tp[i] == ' ') {
        _labelList.add(tp.substring(pos[0] + 1, i));
        _problem.content = tp.substring(i + 1, tp.length);
        break;
      }
    }
    ScreenUtil.init(
      BoxConstraints(
          maxWidth: MediaQuery.of(context).size.width,
          maxHeight: MediaQuery.of(context).size.height),
      designSize: Size(1080, 2340),
    );
    return SafeArea(
      child: Container(
        width: ScreenUtil().screenWidth - ScreenUtil().setWidth(110),
        margin: EdgeInsets.only(
          top: ScreenUtil().setHeight(58),
        ),
        child: Column(
          //mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                ClipOval(
                  child: Image.network(_problem.photo,
                      width: ScreenUtil().setSp(126),
                      height: ScreenUtil().setSp(126),
                      fit: BoxFit.cover),
                ),
                SizedBox(width: ScreenUtil().setWidth(13)),
                Container(
                  height: ScreenUtil().setHeight(140),
                  margin: EdgeInsets.only(left: ScreenUtil().setWidth(20)),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      Text(
                        _problem.name,
                        style: TextStyle(
                          fontSize: ScreenUtil().setSp(46),
                          color: Colors.black,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      Text(
                        _problem.time,
                        style: TextStyle(
                          fontSize: ScreenUtil().setSp(40),
                          color: AppColors.subtext,
                        ),
                      ),
                    ],
                  ),
                ),
                Expanded(
                  child: Align(
                    alignment: Alignment.bottomRight,
                    child: PopupMenuButton(
                      padding: EdgeInsets.zero,
                      itemBuilder: (BuildContext context) {
                        return <PopupMenuItem<String>>[
                          PopupMenuItem<String>(
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                              children: [
                                Icon(MyIcons.guanzhu),
                                Text("关注"),
                              ],
                            ),
                            value: "share",
                          ),
                          PopupMenuItem<String>(
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                              children: [
                                Icon(MyIcons.sixin),
                                Text("私信"),
                              ],
                            ),
                            value: "more",
                          ),
                        ];
                      },
                      onSelected: (String action) {
                        switch (action) {
                          case "share":
                            print(_problem.replyNum);
                            break;
                          case "more":
                            print("更多");
                            break;
                        }
                      },
                    ),
                  ),
                )
              ],
            ),
            Container(
              margin: EdgeInsets.only(
                  //left: ScreenUtil().setWidth(98),
                  top: ScreenUtil().setHeight(46)),
              child: Column(
                //mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: _labelList
                        .asMap()
                        .keys
                        .map((index) => _labelWidget(_labelList[index]))
                        .toList(),
                  ),
                  Container(
                    alignment: Alignment.bottomLeft,
                    padding: EdgeInsets.only(right: 17),
                    child: Text(
                      _problem.content,
                      style: TextStyle(
                        fontSize: ScreenUtil().setSp(46),
                        color: Colors.black,
                      ),
                    ),
                  ),
                  SizedBox(height: ScreenUtil().setHeight(38)),
                  ImgContainer(imgUrl: [
                    'https://img0.baidu.com/it/u=1479071042,4089030983&fm=26&fmt=auto',
                  ]),
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Expanded(
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Row(
                              children: [
                                IconButton(
                                    icon: Icon(
                                      MyIcons.thumb_up_alt,
                                      size: ScreenUtil().setSp(55),
                                      color: AppColors.subtext,
                                    ),
                                    onPressed: () {}),
                                Text(
                                  _problem.thumbsUpNum.toString(),
                                  style: TextStyle(
                                    fontSize: ScreenUtil().setSp(40),
                                    color: AppColors.subtext,
                                  ),
                                ),
                              ],
                            ),
                            Row(
                              children: [
                                IconButton(
                                  icon: Icon(
                                    MyIcons.remark,
                                    size: ScreenUtil().setSp(75),
                                    color: AppColors.subtext,
                                  ),
                                  onPressed: () {},
                                ),
                                Text(
                                  _problem.replyNum.toString(),
                                  style: TextStyle(
                                    fontSize: ScreenUtil().setSp(40),
                                    color: AppColors.subtext,
                                  ),
                                ),
                              ],
                            ),
                            IconButton(
                              icon: Icon(
                                Icons.share_outlined,
                                size: ScreenUtil().setSp(75),
                                color: AppColors.subtext,
                              ),
                              onPressed: () {},
                            ),
                          ],
                        ),
                      )
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _labelWidget(String name) {
    return Container(
      alignment: Alignment.center,
      height: ScreenUtil().setHeight(54),
      //width: 90,
      margin: EdgeInsets.only(right: 7),
      decoration: BoxDecoration(
        color: Color(0xffF5C5A8),
        borderRadius: BorderRadius.only(
          bottomLeft: Radius.circular(ScreenUtil().setHeight(27)),
          bottomRight: Radius.circular(ScreenUtil().setHeight(27)),
          topRight: Radius.circular(ScreenUtil().setHeight(27)),
        ),
      ),
      padding: EdgeInsets.only(left: 6, right: 6),
      child: Text(
        '#$name',
        style: TextStyle(
          fontSize: ScreenUtil().setSp(31),
          color: Colors.white,
        ),
      ),
    );
  }
}

class ImgContainer extends StatelessWidget {
  ImgContainer({Key key, this.imgUrl}) : super(key: key);
  List<String> imgUrl;

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(
      BoxConstraints(
          maxWidth: MediaQuery.of(context).size.width,
          maxHeight: MediaQuery.of(context).size.height),
      designSize: Size(1080, 2340),
    );
    double h=1,w=1,h1=1,w1=1;
    int len = imgUrl.length;
    if(len==0){
      return Container(
        width: 0,
        height: 0,
      );
    }
    else if (len == 1) {
      return ClipRRect(
        child: Image.network(
          imgUrl[0],
          fit: BoxFit.cover,
          width: ScreenUtil().setWidth(570),
          height: ScreenUtil().setHeight(470),
        ),
        borderRadius: BorderRadius.all(Radius.circular(6)),
      );
    } else if (len == 2) {
      return Container(
        child: Row(
          children: [
            ClipRRect(
              borderRadius: BorderRadius.all(Radius.circular(3)),
              child: Image.network(
                imgUrl[0],
                width: ScreenUtil().setWidth(300),
                height: ScreenUtil().setWidth(300),
                fit: BoxFit.cover,
              ),
            ),
            SizedBox(
              width: ScreenUtil().setWidth(20),
            ),
            ClipRRect(
              borderRadius: BorderRadius.all(Radius.circular(3)),
              child: Image.network(
                imgUrl[0],
                width: ScreenUtil().setWidth(300),
                height: ScreenUtil().setWidth(300),
                fit: BoxFit.cover,
              ),
            ),
          ],
        ),
      );
    } else if (len == 3) {
      return Container(
        width: ScreenUtil().setWidth(880),
        height: ScreenUtil().setHeight(445),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            ClipRRect(
              borderRadius: BorderRadius.all(Radius.circular(4)),
              child: Image.network(
                imgUrl[0],
                width: ScreenUtil().setWidth(553),
                height: ScreenUtil().setHeight(430),
                fit: BoxFit.cover,
              ),
            ),
            Container(
              width: ScreenUtil().setWidth(302),
              height: ScreenUtil().setHeight(430),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  ClipRRect(
                    borderRadius: BorderRadius.all(Radius.circular(4)),
                    child: Image.network(
                      imgUrl[1],
                      width: ScreenUtil().setWidth(302),
                      height: ScreenUtil().setHeight(202),
                      fit: BoxFit.cover,
                    ),
                  ),
                  ClipRRect(
                    borderRadius: BorderRadius.all(Radius.circular(4)),
                    child: Image.network(
                      imgUrl[2],
                      fit: BoxFit.cover,
                      width: ScreenUtil().setWidth(302),
                      height: ScreenUtil().setHeight(202),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      );
    } else if (len == 4) {
      return Container(
        width: ScreenUtil().setWidth(920),
        height: ScreenUtil().setHeight(720),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Container(
              width: ScreenUtil().setWidth(920),
              height: ScreenUtil().setHeight(345),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  ClipRRect(
                    borderRadius: BorderRadius.all(Radius.circular(3)),
                    child: Image.network(
                      imgUrl[1],
                      width: ScreenUtil().setWidth(447),
                      height: ScreenUtil().setHeight(345),
                      fit: BoxFit.cover,
                    ),
                  ),
                  ClipRRect(
                    borderRadius: BorderRadius.all(Radius.circular(3)),
                    child: Image.network(
                      imgUrl[2],
                      width: ScreenUtil().setWidth(447),
                      height: ScreenUtil().setHeight(345),
                      fit: BoxFit.cover,
                    ),
                  ),
                ],
              ),
            ),
            Container(
              width: ScreenUtil().setWidth(940),
              height: ScreenUtil().setHeight(345),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  ClipRRect(
                    borderRadius: BorderRadius.all(Radius.circular(3)),
                    child: Image.network(
                      imgUrl[1],
                      width: ScreenUtil().setWidth(447),
                      height: ScreenUtil().setHeight(345),
                      fit: BoxFit.cover,
                    ),
                  ),
                  ClipRRect(
                    borderRadius: BorderRadius.all(Radius.circular(3)),
                    child: Image.network(
                      imgUrl[2],
                      width: ScreenUtil().setWidth(447),
                      height: ScreenUtil().setHeight(345),
                      fit: BoxFit.cover,
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      );
    } else if (len == 5) {
      return Container(
        width: ScreenUtil().setWidth(890*w1),
        height: ScreenUtil().setHeight(815*h1),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Container(
              width: ScreenUtil().setWidth(489*w1),
              height: ScreenUtil().setHeight(778*h1),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  ClipRRect(
                    borderRadius: BorderRadius.all(Radius.circular(4)),
                    child: Image.network(
                      imgUrl[0],
                      fit: BoxFit.cover,
                      width: ScreenUtil().setWidth(489*w1),
                      height: ScreenUtil().setHeight(374*h1),
                    ),
                  ),
                  ClipRRect(
                    borderRadius: BorderRadius.all(Radius.circular(4)),
                    child: Image.network(
                      imgUrl[1],
                      fit: BoxFit.cover,
                      width: ScreenUtil().setWidth(489*w1),
                      height: ScreenUtil().setHeight(374*h1),
                    ),
                  ),
                ],
              ),
            ),
            Container(
              height: ScreenUtil().setHeight(778*h1),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  ClipRRect(
                    borderRadius: BorderRadius.all(Radius.circular(4)),
                    child: Image.network(
                      imgUrl[2],
                      fit: BoxFit.cover,
                      width: ScreenUtil().setWidth(374*w1),
                      height: ScreenUtil().setHeight(242*h1),
                    ),
                  ),
                  ClipRRect(
                    borderRadius: BorderRadius.all(Radius.circular(4)),
                    child: Image.network(
                      imgUrl[3],
                      fit: BoxFit.cover,
                      width: ScreenUtil().setWidth(374*w1),
                      height: ScreenUtil().setHeight(242*h1),
                    ),
                  ),
                  ClipRRect(
                    borderRadius: BorderRadius.all(Radius.circular(4)),
                    child: Image.network(
                      imgUrl[4],
                      fit: BoxFit.cover,
                      width: ScreenUtil().setWidth(374*w1),
                      height: ScreenUtil().setHeight(242*h1),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      );
    } else {
      return Container(
        height: ScreenUtil().setHeight(893*h),
        width: ScreenUtil().setWidth(912*w),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Container(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  ClipRRect(
                    borderRadius: BorderRadius.all(Radius.circular(4)),
                    child: Image.network(
                      imgUrl[0],
                      fit: BoxFit.cover,
                      width: ScreenUtil().setWidth(594*w),
                      height: ScreenUtil().setHeight(584*h),
                    ),
                  ),
                  Container(
                    height: ScreenUtil().setHeight(281*h),
                    width: ScreenUtil().setWidth(604*w),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        ClipRRect(
                          borderRadius: BorderRadius.all(Radius.circular(4)),
                          child: Image.network(
                            imgUrl[1],
                            fit: BoxFit.cover,
                            width: ScreenUtil().setWidth(285*w),
                            height: ScreenUtil().setHeight(280*h),
                          ),
                        ),
                        ClipRRect(
                          borderRadius: BorderRadius.all(Radius.circular(4)),
                          child: Image.network(
                            imgUrl[2],
                            fit: BoxFit.cover,
                            width: ScreenUtil().setWidth(285*w),
                            height: ScreenUtil().setHeight(280*h),
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            Container(
              height: ScreenUtil().setHeight(940*h),
              width: ScreenUtil().setWidth(283*w),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  ClipRRect(
                    borderRadius: BorderRadius.all(Radius.circular(4)),
                    child: Image.network(
                      imgUrl[3],
                      fit: BoxFit.cover,
                      width: ScreenUtil().setWidth(283*w),
                      height: ScreenUtil().setHeight(280*h),
                    ),
                  ),
                  ClipRRect(
                    borderRadius: BorderRadius.all(Radius.circular(4)),
                    child: Image.network(
                      imgUrl[4],
                      fit: BoxFit.cover,
                      width: ScreenUtil().setWidth(283*w),
                      height: ScreenUtil().setHeight(280*h),
                    ),
                  ),
                  ClipRRect(
                    borderRadius: BorderRadius.all(Radius.circular(4)),
                    child: Image.network(
                      imgUrl[5],
                      fit: BoxFit.cover,
                      width: ScreenUtil().setWidth(283*w),
                      height: ScreenUtil().setHeight(280*h),
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      );
    }
  }
}
